/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package towerdefence.view;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureCoords;
import java.awt.Color;
import javax.media.opengl.GL;
import net.phys2d.math.ROVector2f;
import net.phys2d.raw.shapes.Box;
import towerdefence.model.Builder;
import static javax.media.opengl.GL.*;

/**
 *
 * @author Andreas
 */
public class GLBuilderRenderer {

    public static void drawBuilder(GL gl, Builder builder, Color color) {
        Box box = (Box) builder.getShape();
        ROVector2f size = box.getSize();
        ROVector2f position = builder.getPosition();
        float right = size.getX() / 2;
        float left = -right;
        float top = -size.getY() / 2;
        float bottom = -top + 10;

        gl.glPushMatrix();
        gl.glColor4fv(color.getRGBComponents(null), 0);
        gl.glTranslatef(position.getX(), position.getY(), 0);
        gl.glRotatef(GLCore.toDegrees(builder.getRotation()), 0, 0, 1.0f);
        Texture texture = TextureHandler.getTexture(TextureHandler.BUILDER);
        texture.enable();
        texture.bind();

        TextureCoords coords = texture.getImageTexCoords();

        gl.glBegin(GL_QUADS);
        {
            gl.glTexCoord2f(coords.left(), coords.top());
            gl.glVertex2f(left, top);
            gl.glTexCoord2f(coords.right(), coords.top());
            gl.glVertex2f(right, top);
            gl.glTexCoord2f(coords.right(), coords.bottom());
            gl.glVertex2f(right, bottom);
            gl.glTexCoord2f(coords.left(), coords.bottom());
            gl.glVertex2f(left, right);
        }
        gl.glEnd();
        texture.disable();
        gl.glPopMatrix();
    }
}
